﻿using Coolzon.Common;
using Coolzon.Realtime.Contracts;
using Coolzon.UI.Core.Runtime;
using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coolzon.UI.Common.RealtimeProxies
{
    /// <summary>
    /// 远程的事件代理
    /// </summary>
    public partial class RemoteEventProxy : IMainServiceCallback
    {
        private object s_lockReceiveRequest = new object();
        private Dictionary<Guid, int> _receiveFlag = new Dictionary<Guid, int>();

        public void UserEnterControlDomain(string controlDomain, UserInfo user)
        {
            throw new NotImplementedException();
        }

        public IAsyncResult BeginUserEnterControlDomain(string controlDomain, UserInfo user, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndUserEnterControlDomain(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 收到来自服务器的事件
        /// </summary>
        /// <param name="response"></param>
        public void OnReceiveResponse(Response response)
        {
            //Common.Logger.InfoToTag("RemoteEventProxy", "接收到指令scenario:0x{0:X2},command:0x{1:X2},UniqueKey:{2},Server:{3},ExpectantResponseCount:{4}",
            //    response.Header.RequestHeader.Scenario,
            //    response.Header.RequestHeader.Command,
            //    response.Header.RequestHeader.UniqueKey,
            //    response.Header.RequestHeader.ServerAddress,
            //    response.Header.RequestHeader.ExpectantResponseCount);

            if (response.Header == null
                || response.Header.RequestHeader == null
                || response.Header.RequestHeader.UniqueKey == Guid.Empty)
            {
                IResponseHandler handler = (IResponseHandler)ResponseHandlerFactory.CreateHandler(response.Header.Scenario, response.Header.Command);
                handler.OnHandle(this, response);
                Logger.InfoToTag("RemoteEventProxy", string.Format("没有Request的Response执行指令完毕scenario:0x{0:X2},command:0x{1:X2}",
                    response.Header.Scenario,
                    response.Header.Command));
            }
            else if (RealtimeProxyManager.Instance.ResponseKeeper.IsUseable(RealtimeProxyManager.Instance,
                response.Header.RequestHeader.UniqueKey,
                response.Header.RequestHeader.ServerAddress,
                response.Header.RequestHeader.ExpectantResponseCount))
            {
                try
                {
                    IResponseHandler handler = (IResponseHandler)ResponseHandlerFactory.CreateHandler(response.Header.Scenario, response.Header.Command);
                    handler.OnHandle(this, response);
                    Logger.DebugToTag("RemoteEventProxy", string.Format("执行指令完毕scenario:0x{0:X2},command:0x{1:X2},UniqueKey:{2},Server:{3},ExpectantResponseCount:{4}",
                        response.Header.RequestHeader.Scenario,
                        response.Header.RequestHeader.Command,
                        response.Header.RequestHeader.UniqueKey,
                        response.Header.RequestHeader.ServerAddress,
                        response.Header.RequestHeader.ExpectantResponseCount));
                }
                catch (Exception ex)
                {
                    Logger.DebugToTag("RemoteEventProxy", string.Format("执行指令出错scenario:0x{0:X2},command:0x{1:X2},UniqueKey:{2},Server:{3},ExpectantResponseCount:{4},ex:{5}",
                        response.Header.RequestHeader.Scenario,
                        response.Header.RequestHeader.Command,
                        response.Header.RequestHeader.UniqueKey,
                        response.Header.RequestHeader.ServerAddress,
                        response.Header.RequestHeader.ExpectantResponseCount,
                        ex));
                }
            }
            else
            {
                Logger.WarningToTag("RemoteEventProxy", string.Format("接收到指令scenario:0x{0:X2},command:0x{1:X2},UniqueKey:{2},Server:{3},ExpectantResponseCount:{4},但没有执行",
                    response.Header.RequestHeader.Scenario,
                    response.Header.RequestHeader.Command,
                    response.Header.RequestHeader.UniqueKey,
                    response.Header.RequestHeader.ServerAddress,
                    response.Header.RequestHeader.ExpectantResponseCount));
            }
        }

        public IAsyncResult BeginOnReceiveResponse(Response response, AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public void EndOnReceiveResponse(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        public ConnectionState TestConnectionState()
        {
            ConnectionState conState = new ConnectionState();
            conState.IsSuccess = true;
            return conState;
        }

        public IAsyncResult BeginTestConnectionState(AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public ConnectionState EndTestConnectionState(IAsyncResult result)
        {
            throw new NotImplementedException();
        }


        public QueryViewStateResult QueryViewState()
        {
            QueryViewStateResult result = new QueryViewStateResult();
            //if (this.RenderView != null)
            //{
            //    result.ViewStateToken = this.RenderView.GetViewStateToken();
            //    ViewState viewState = this.RenderView.GetViewState();
            //    if (viewState != null)
            //    {
            //        result.ViewStateSolution = viewState.SolutionName;
            //        result.ViewStateAge = viewState.Age;
            //    }
            //}
            return result;
        }

        public IAsyncResult BeginQueryViewState(AsyncCallback callback, object asyncState)
        {
            throw new NotImplementedException();
        }

        public QueryViewStateResult EndQueryViewState(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        public void TestConnect()
        {

        }


        public void TestConnection()
        {
            throw new NotImplementedException();
        }
    }
}
